<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                              |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: anguolei@clcw.com.cn                                           |
// +------------------------------------------------------------------------+
//
namespace AuctionCore\AuctionOrder;

use AuctionCore\CarResource\Car;
use AuctionCore\Other\Store;
use AuctionCore\System\AuctionException;
use AuctionCore\CarResource\CarModel;
use AuctionCore\Other\Address;
use AuctionCore\ChangeOrder\ChangeOrder;
/**
 * 回车类
 * @author Angl
 */
class CarReturn
{
    /**
     * 回车记录
     * @param $data
     * @return mixed
     */
    public static function create($data)
    {
        $data['create_time'] = date('Y-m-d H:i:s');
        $id = M('car_return_log')->add($data);
        return $id;
    }

    /**
     * 通过车辆id更新
     * @param $car_id
     * @param $data
     * @return
     */
    public function update_by_order_id($order_id,$data)
    {
        if($order_id<1 || empty($data)) return false;
        return M('car_return_log')->where('order_id='.$order_id)->save($data);
    }

    /**
     * 获取回车详细信息
     * @param $car_id
     * @return bool|mixed
     */
    public static function get_detail_by_order_id($order_id)
    {
        if(intval($order_id)<1) return false;
        return M('car_return_log')->where("order_id=".$order_id)->find();
    }

    /**
     * 获取列表
     * @param $page
     * @param $limit
     * @param $location_area
     * @param $order_no
     * @param $driver
     * @param $driver_phone
     * @return array
     */
    public function get_list($page, $limit, $pay_status, $status, $location_area, $order_no, $checker, $driver, $driver_phone, $delive_store, $auth,$start_time,$end_time)
    {
        $page = intval($page) > 0 ? intval($page) : 1;
        $limit = intval($limit) > 0 ? intval($limit) : 10;
        $map = [];
        $map['crl.status'] = intval($status);
        if($status==2){
            $order = "crl.arrival_time DESC";
        }
        //付款状态 -- 4s店车只有首款状态
        if($pay_status == 1){                           //待付首款
            $map['c.pay_status'] = 1;
        }elseif($pay_status == 2){                      //已付首款
            $map['c.pay_status'] = 2;
        }
        //待回车和回车中必须是已签约状态 8
        if($status<2){
            $map['o.status'] = 8;
        }
        //所在地
        if(!empty($location_area)){
            $map['c.location_area'] = array('in',$location_area);
        }
        //拍单号
        if(!empty($order_no)){
            $map['o.order_no'] = $order_no;
        }

        if($auth==true){
            //评估师
            if(!empty($checker)){
                $checker_id = M('employee')->where("real_name='".trim($checker)."'")->getField('emp_id');
                $map['co.checker_id'] = $checker_id;
            }
        }else{
            $map['co.checker_id'] = session("user_info")['emp_id'];
        }
        //代驾
        if(!empty($driver)){
            $map['crl.driver_name'] = $driver;
        }
        //代驾电话
        if(!empty($driver_phone)){
            $map['crl.driver_phone'] = $driver_phone;
        }
        //交付门店
        if(!empty($delive_store)){
            $map['crl.delive_store'] = $delive_store;
        }
        //到店时间 arrival_time
        if ($start_time) {
            $map['crl.arrival_time'] = array('egt', $start_time . ' 00:00:00');
        }
        if ($end_time) {
            $map['crl.arrival_time'] = array('elt', $end_time . ' 23:59:59');
        }
        if ($start_time && $end_time) {
            $map['crl.arrival_time'] = array(array('egt', $start_time . ' 00:00:00'), array('elt', $end_time . ' 23:59:59'));
        }

        $cols = "o.order_id,o.order_no,o.status,o.first_pay_status,o.is_lock,c.location_area,co.checker_id,
            c.car_id,c.brand_id,c.series_id,c.model_id,c.plate_prefix,c.plate_suffix,c.first_money,c.tail_money,c.car_source,c.pay_status,c.three_in_one,
            crl.driver_name,crl.driver_phone,crl.delive_store,crl.arrival_time,crl.depart_time,crl.status as return_status,crl.create_time";
        $prefix = C("DB_PREFIX");
        $total = M('car_return_log')->alias("crl")
            ->join($prefix."order as o ON o.order_id=crl.order_id")
            ->join($prefix."cars as c ON c.car_id=o.car_id")
            ->join($prefix."car_owner as co ON co.owner_id=c.owner_id")
            ->where($map)->count();

        $list =M('car_return_log')->alias("crl")->field($cols)
            ->join($prefix."order as o ON o.order_id=crl.order_id")
            ->join($prefix."cars as c ON c.car_id=o.car_id")
            ->join($prefix."car_owner as co ON co.owner_id=c.owner_id")
            ->where($map)->page($page)->limit($limit)->order($order)->select();
//        echo M()->getLastSql();
        if(!empty($list)){
            $cm = new CarModel();
            $addr = new Address();
            foreach ($list as $key => $vl) {
                //车型
                $brand = $cm->get_model_by_id($vl['brand_id'], 'brand');
                $series = $cm->get_model_by_id($vl['series_id'], 'series');
                $model = $cm->get_model_by_id($vl['model_id']);
                $list[$key]['car_model'] = $brand['name'] . $series['name'] . $model['name'];
                //所在地
                if ($vl['location_area']) {
                    $location_area = $addr->get_name_by_code($vl['location_area']);
                    $list[$key]['location_area'] = $location_area;
                } else {
                    $list[$key]['location_area'] = '无';
                }
                //车牌号
                $list[$key]['plate_number'] = C("PLATE_PREFIX.".$vl['plate_prefix']).$vl['plate_suffix'];
                //评估师
                if($vl['return_status']==0 && $vl['checker_id']>0){
                    $checker = M("employee")->where("emp_id=".$vl['checker_id'])->find();
                    $list[$key]['checker_name']=$checker['real_name'];
                    $list[$key]['checker_phone'] = $checker['mobile'];
                }
                if($status>0){
                    $store = new Store();
                    $store_info = $store->detail(['store_id'=>$vl['delive_store']]);
                    $list[$key]['delive_store_str'] = $store_info['store_name'];
                }
                //是否显示违约
                $list[$key]['is_again_void'] = true;
                // $changeInfo = changeOrder::detailMap(['order_id'=>$vl['order_id'],'order_status'=>$vl['status']]);
                // if ($changeInfo) {
                //     $list[$key]['is_again_void'] = false;
                // }
                if ($vl['is_lock'] == 1) {
                    $list[$key]['is_again_void'] = false;
                }
                //付款状态
                $list[$key]['pay_status'] = $vl['pay_status']==1 ? "待付款" : "已付款";
            }
        }

        $data = ['total'=>$total,'list'=>$list];
        return $data;
    }

    /**
     * 根据sid获取回车信息
     * @param $sid
     * @return mixed
     */
    public static function getCarReturnInfoBySid($sid)
    {
        $map = ['sid' => $sid];
        return M("car_return_log")->where($map)->find();
    }
}

//end class
